<?php

class UcPreviewListController extends Controller
{
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/

    /*
    * 添加
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");
            $pic_url = Util::getParamNotEmpty("pic_url");
            $worker_id = Util::getParamNotEmpty("worker_id");
            $type_id = Util::getParamNotEmpty("type_id");
            $view_count = Util::getParam("view_count",0);
            $order_count = Util::getParam("order_count",0);




            //添加
            $obj = new UcPreviewList();
            Util::initObjData($obj);


            $obj->name = $name;
            $obj->pic_url = $pic_url;
            $obj->worker_id = $worker_id;
            $obj->type_id = $type_id;
            $obj->view_count = $view_count;
            $obj->order_count = $order_count;




            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 修改
    *
    */
    public function actionUpdate()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $name = Util::getParamNotEmpty("name");
            $pic_url = Util::getParamNotEmpty("pic_url");
            $worker_id = Util::getParamNotEmpty("worker_id");
            $type_id = Util::getParamNotEmpty("type_id");
            $view_count = Util::getParam("view_count",0);
            $order_count = Util::getParam("order_count",0);


            //验证存在
            $obj = UcPreviewList::model()->findByPK($obj_id);

            Util::checkExistAndNotDelete($obj);

            //修改信息

            $obj->name = $name;
            $obj->pic_url = $pic_url;
            $obj->worker_id = $worker_id;
            $obj->type_id = $type_id;
            $obj->view_count = $view_count;
            $obj->order_count = $order_count;

            //保存
            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 删除
    */
    public function actionDelete()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //删除
            $obj = UcPreviewList::model()->findByPK($obj_id);

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 获取列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数列表
            $type_id = Util::getParamNotEmpty("type_id");


            $searchsql = Util::initSearchSql();


            $searchsql = Util::getSerarchStr($searchsql, "type_id", "type_id");


            //获取count
            $totalcount = UcPreviewList::model()->count($searchsql);



                //排序
                $searchsql = $searchsql . " order by create_time desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = UcPreviewList::model()->findAll($searchsql);

                //插入worker信息
                foreach($list as $key => $value){
                    $list[$key]->worker = WorkerService::formatWorker(Worker ::model()->findByPK($list[$key] -> worker_id));
                }


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }
}